﻿using System;
using System.Data;
using System.Collections.Generic;
using Maticsoft.Common;
using Lawyer.Model;
namespace Lawyer.BLL
{
	/// <summary>
	/// ArchiveInfo
	/// </summary>
	public partial class ArchiveInfo
	{
		private readonly Lawyer.DAL.ArchiveInfo dal=new Lawyer.DAL.ArchiveInfo();
		public ArchiveInfo()
		{}
		#region  Method
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string ArchiveID)
		{
			return dal.Exists(ArchiveID);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Lawyer.Model.ArchiveInfo model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Lawyer.Model.ArchiveInfo model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string ArchiveID)
		{
			
			return dal.Delete(ArchiveID);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string ArchiveIDlist )
		{
			return dal.DeleteList(ArchiveIDlist );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Lawyer.Model.ArchiveInfo GetModel(string ArchiveID)
		{
			
			return dal.GetModel(ArchiveID);
		}

		/// <summary>
		/// 得到一个对象实体，从缓存中
		/// </summary>
		public Lawyer.Model.ArchiveInfo GetModelByCache(string ArchiveID)
		{
			
			string CacheKey = "ArchiveInfoModel-" + ArchiveID;
			object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(ArchiveID);
					if (objModel != null)
					{
						int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
						Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (Lawyer.Model.ArchiveInfo)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Lawyer.Model.ArchiveInfo> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Lawyer.Model.ArchiveInfo> DataTableToList(DataTable dt)
		{
			List<Lawyer.Model.ArchiveInfo> modelList = new List<Lawyer.Model.ArchiveInfo>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				Lawyer.Model.ArchiveInfo model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = new Lawyer.Model.ArchiveInfo();
					if(dt.Rows[n]["ArchiveID"]!=null && dt.Rows[n]["ArchiveID"].ToString()!="")
					{
					model.ArchiveID=dt.Rows[n]["ArchiveID"].ToString();
					}
					if(dt.Rows[n]["Title"]!=null && dt.Rows[n]["Title"].ToString()!="")
					{
					model.Title=dt.Rows[n]["Title"].ToString();
					}
                    if (dt.Rows[n]["LawyerID"] != null && dt.Rows[n]["LawyerID"].ToString() != "")
                    {
                        model.LawyerID = dt.Rows[n]["LawyerID"].ToString();
                        Model.UserInfo user = (new BLL.UserInfo()).GetModelFromUserID(model.LawyerID);
                        if (user != null)
                        {
                            model.LawyerName = user.UserName;
                        }
                        else {
                            model.LawyerName = "None";
                        }
                    }
                    else {
                        model.LawyerName = "None";
                    }
					if(dt.Rows[n]["CustomID"]!=null && dt.Rows[n]["CustomID"].ToString()!="")
					{
					    model.CustomID=dt.Rows[n]["CustomID"].ToString();
                        Lawyer.Model.CustomInfo custom = (new Lawyer.BLL.CustomInfo()).GetModel(model.CustomID);
                        if(custom != null)
                        {
                            model.CustomName = custom.FullName;
                        }
					}
					if(dt.Rows[n]["AgencyID"]!=null && dt.Rows[n]["AgencyID"].ToString()!="")
					{
					    model.AgencyID=dt.Rows[n]["AgencyID"].ToString();
                        
					}
                    if(dt.Rows[n]["IsAgency"]!=null && dt.Rows[n]["IsAgency"].ToString()!="")
					{
						model.IsAgency=int.Parse(dt.Rows[n]["IsAgency"].ToString());
                        if (model.IsAgency == 1)
                        {
                            if (model.AgencyID != null && model.AgencyID != "")
                            {
                                Lawyer.Model.AgencyInfo agency = (new Lawyer.BLL.AgencyInfo()).GetModel(model.AgencyID);
                                if (agency != null)
                                {
                                    model.AgencyName = agency.Name;
                                }
                            }
                        }
                        else {
                            model.AgencyName = "None";
                        }
					}
					if(dt.Rows[n]["AgencyRate"]!=null && dt.Rows[n]["AgencyRate"].ToString()!="")
					{
						model.AgencyRate=decimal.Parse(dt.Rows[n]["AgencyRate"].ToString());
					}
					if(dt.Rows[n]["Status"]!=null && dt.Rows[n]["Status"].ToString()!="")
					{
						model.Status=int.Parse(dt.Rows[n]["Status"].ToString());
                        if (model.Status == 0) {
                            model.ShowStatus = "Pending";
                        }
                        else if (model.Status == 1)
                        {
                            model.ShowStatus = "Assigned to me";
                        }
                        else if (model.Status == 2)
                        {
                            model.ShowStatus = "Ongoing";
                        }
                        else if (model.Status == 3)
                        {
                            model.ShowStatus = "Close approving";
                        }
                        else if (model.Status == 4)
                        {
                            model.ShowStatus = "Closed";
                        }
					}
					if(dt.Rows[n]["EndDate"]!=null && dt.Rows[n]["EndDate"].ToString()!="")
					{
						model.EndDate=DateTime.Parse(dt.Rows[n]["EndDate"].ToString());
					}
					if(dt.Rows[n]["CreatedDate"]!=null && dt.Rows[n]["CreatedDate"].ToString()!="")
					{
						model.CreatedDate=DateTime.Parse(dt.Rows[n]["CreatedDate"].ToString());
					}
					if(dt.Rows[n]["CreatedBy"]!=null && dt.Rows[n]["CreatedBy"].ToString()!="")
					{
					model.CreatedBy=dt.Rows[n]["CreatedBy"].ToString();
					}
					if(dt.Rows[n]["CheckedBy"]!=null && dt.Rows[n]["CheckedBy"].ToString()!="")
					{
					model.CheckedBy=dt.Rows[n]["CheckedBy"].ToString();
					}
					if(dt.Rows[n]["CheckedDate"]!=null && dt.Rows[n]["CheckedDate"].ToString()!="")
					{
						model.CheckedDate=DateTime.Parse(dt.Rows[n]["CheckedDate"].ToString());
					}
					if(dt.Rows[n]["UpdatedDate"]!=null && dt.Rows[n]["UpdatedDate"].ToString()!="")
					{
						model.UpdatedDate=DateTime.Parse(dt.Rows[n]["UpdatedDate"].ToString());
					}
					if(dt.Rows[n]["UpdatedBy"]!=null && dt.Rows[n]["UpdatedBy"].ToString()!="")
					{
					model.UpdatedBy=dt.Rows[n]["UpdatedBy"].ToString();
					}
                    //添加总额
                    //
                    decimal count = 0;
                    List<Lawyer.Model.ActivityInfo> activitylist = (new Lawyer.BLL.ActivityInfo()).GetModelList(" ArchiveID ='" + model.ArchiveID + "'");
                    foreach (var item in activitylist)
                    {
                        count += item.Fee.Value;
                    }
                    Lawyer.Model.ArchiveBaseInfo baseinfo = (new Lawyer.BLL.ArchiveBaseInfo()).GetModel(model.ArchiveID);
                    if (baseinfo != null&&baseinfo.AgreedFees.HasValue)
                    {
                        count += baseinfo.AgreedFees.Value;
                    }
                    model.MoneyCount = double.Parse(count.ToString());


					modelList.Add(model);
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  Method
	}
}

